<script setup lang="ts">
import { ref, onMounted } from 'vue';

const useLastChanged = () => {
  const lastUpdateTime = ref(new Date().getTime());

  const updateLastUpdateTime = () => {
    // 获取时间戳
    lastUpdateTime.value = new Date().getTime();
  };

  onMounted(() => {
    // 监听页面的交互事件，比如点击、输入等，更新最后变更时间戳
    document.addEventListener('click', updateLastUpdateTime);
    document.addEventListener('input', updateLastUpdateTime);
  });

  return {
    lastUpdateTime,
  };
};

export default useLastChanged;
</script>
<template>
  <div>
    <p>上一次更新的时间戳: {{ new Date(lastUpdateTime).toLocaleString() }}</p>
  </div>
</template>

<script>
import { defineComponent } from 'vue';
import useLastUpdateTime from './useLastUpdateTime';

export default defineComponent({
  setup() {
    const { lastUpdateTime } = useLastChanged();

    return {
      lastUpdateTime,
    };
  },
});
</script>